package com.huaxin.hxcloud.work;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.huaxin.common.Entity.HxMaintenanceFirstDTO;
import com.huaxin.common.utils.ToastUtil;
import com.huaxin.hxcloud.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author songxf
 * @date 2018/6/8
 * @Desc SPT运维分析树形结构adapter
 * Copyright HUAXIN DATA. All rights reserved.
 */
public class HxSPTMaintenanceAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {
    public static final  int          orgAreaTierOne   = 0;
    public static final  int          orgAreaTierTwo   = 1;
    public static final  int          orgAreaTierThree = 2;
    private static final String       TAG              = "HxSPTMaintenanceAdapter==>  ";
    private              List<String> mFlagList        = new ArrayList<>();

    public void setOnSelectListListener(onSelectListListener onSelectListListener) {
        this.mOnSelectListListener = onSelectListListener;
    }

    private onSelectListListener mOnSelectListListener;

    public interface onSelectListListener {
        void getSelectList(List<String> selects);
    }


    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public HxSPTMaintenanceAdapter(List<MultiItemEntity> data) {
        super(data);
        addItemType(orgAreaTierOne, R.layout.item_spt_maintenance_first);
        addItemType(orgAreaTierTwo, R.layout.item_spt_maintenance_second);
        addItemType(orgAreaTierThree, R.layout.item_spt_maintenance_three);
    }

    @Override
    protected void convert(final BaseViewHolder helper, final MultiItemEntity item) {
        final HxMaintenanceFirstDTO firstDTO = (HxMaintenanceFirstDTO) item;
        switch (helper.getItemViewType()) {
            case orgAreaTierOne:
                TextView nameTv = helper.getView(R.id.name_tv);
                ImageView mfirstSelectImg = helper.getView(R.id.expand_main_arrow_img);
                ImageView firstSelectCk = helper.getView(R.id.select_img);
                nameTv.setText(firstDTO.getName());
                if (firstDTO.getChildren() == null) {
                    mfirstSelectImg.setVisibility(View.GONE);
                } else {
                    mfirstSelectImg.setVisibility(View.VISIBLE);
                }
                if (firstDTO.isExpanded()) {
                    helper.setImageResource(R.id.expand_main_arrow_img, R.drawable.arrow_blue_r);
                } else {
                    helper.setImageResource(R.id.expand_main_arrow_img, R.drawable.arrow_blue_b);
                }
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        int pos = helper.getAdapterPosition();
                        if (firstDTO.getChildren() != null && firstDTO.getChildren().size() == 0) {
                            return;
                        }
                        if (firstDTO.isExpanded()) {
                            collapse(pos);
                        } else {
                            expand(pos);
                        }
                        notifyDataSetChanged();
                    }
                });
                onCheckBoxChanger(firstSelectCk, firstDTO);
                break;
            case orgAreaTierTwo:
                TextView secondTv = helper.getView(R.id.tv_second);
                ImageView mSecondSelectImg = helper.getView(R.id.img_second_selcet);
                ImageView mSecondSelectCb = helper.getView(R.id.select_img);
                secondTv.setText(firstDTO.getName());
                if (firstDTO.getChildren() == null) {
                    mSecondSelectImg.setVisibility(View.GONE);
                } else {
                    mSecondSelectImg.setVisibility(View.VISIBLE);
                }
                if (firstDTO.isExpanded()) {
                    helper.setImageResource(R.id.img_second_selcet, R.drawable.arrow_blue_r);
                } else {
                    helper.setImageResource(R.id.img_second_selcet, R.drawable.arrow_blue_b);
                }
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        int pos = helper.getAdapterPosition();
                        if (firstDTO.getChildren() != null && firstDTO.getChildren().size() == 0) {
                            return;
                        }
                        if (firstDTO.isExpanded()) {
                            collapse(pos);
                        } else {
                            expand(pos);
                        }
                        notifyDataSetChanged();
                    }
                });
                onCheckBoxChanger(mSecondSelectCb, firstDTO);
                break;
            case orgAreaTierThree:
                TextView threeTv = helper.getView(R.id.tv_three);
                ImageView mThreeSelectCb = helper.getView(R.id.select_img);
                threeTv.setText(firstDTO.getName());
                onCheckBoxChanger(mThreeSelectCb, firstDTO);
                break;
            default:
                break;

        }
    }

    private void onCheckBoxChanger(final ImageView selectImg, final HxMaintenanceFirstDTO firstDTO) {
        if (firstDTO.isCheck()) {
            selectImg.setImageResource(R.drawable.check_pressed);
        } else {
            selectImg.setImageResource(R.drawable.check_normal);
        }
        selectImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (firstDTO.isCheck()) {
                    firstDTO.setCheck(!firstDTO.isCheck());
                } else if (mFlagList.size() < 3) {
                    firstDTO.setCheck(!firstDTO.isCheck());
                } else {
                    ToastUtil.showToast("选择不能超过3个");
                    return;
                }
                if (firstDTO.isCheck()) {
                    mFlagList.add(firstDTO.getId());
                } else {
                    mFlagList.remove(firstDTO.getId());
                }
                mOnSelectListListener.getSelectList(mFlagList);
                notifyDataSetChanged();
            }
        });

    }
}
